package ph.com.gs3.formalistics.global.utilities;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;

@SuppressLint("DefaultLocale")
public class JSONUtilities {

	public static String cleanupRawJSONArray(String rawJSONArray) {

		String cleanJSONArray = "[]";

		if (rawJSONArray != null) {
			if (!rawJSONArray.isEmpty() && !"NULL".equals(rawJSONArray.toUpperCase().trim())) {
				// the array's clean
				cleanJSONArray = rawJSONArray;
			}
		}

		return cleanJSONArray;

	}

	public static String cleanupRawJSON(String rawJSON) {

		String cleanJSONArray = "{}";

		if (rawJSON != null) {
			if (!rawJSON.isEmpty() && !"NULL".equals(rawJSON.toUpperCase().trim())) {
				// the array's clean
				cleanJSONArray = rawJSON;
			}
		}

		return cleanJSONArray;

	}

	public static JSONArray getJSONArrayFromJSONObject(JSONObject jsonObject, String key)
	        throws JSONException {

		JSONArray jsonArray;

		// throws JSONException
		String rawKeyValue = jsonObject.getString(key);
		if (rawKeyValue.isEmpty() || "NULL".equals(rawKeyValue.toUpperCase().trim())) {
			jsonArray = new JSONArray("[]");
		} else {
			jsonArray = new JSONArray(rawKeyValue);
		}

		return jsonArray;

	}

}
